<?php

namespace App\Http\Controllers;

use App\PolicyConsult;
use App\SpaceList;
use App\Appointment;
use App\Feedback;
use App\OrderBigSpace;
use App\OrderMeetingRoom;
use App\OrderWorkSite;
use App\Post;
use App\ProviderDetail;
use App\ProviderList;
use App\ProviderType;
use App\SpaceRoom;
use App\SpaceWorkSite;
use App\FreeUser;
use App\FreeBill;
use App\Visitor;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class WXController extends Controller
{
    //小程序新使用部分接口
	//获取空间列表
    public function GetSpace()
    {
	$space = SpaceList::get();
        return response()->json(['data'=>$space]);
    }
	//获取会议室
    public function GetMeetingRoom(Request $request)
    {
        $data = $request->input();
        $spaceName = $data['spaceName'];
        if($spaceName=='all')
        {
            $room = SpaceRoom::where("big",'0')->get();
            return response()->json(['data'=>$room]);
        }
        $room = SpaceRoom::where("space_name",$spaceName)->where("big",'0')->get();
        return response()->json(['data'=>$room]);
    }
	//获取工位
    public function GetWorkSite(Request $request)
    {
        $data = $request->input();
        $spaceName = $data['spaceName'];
        if($spaceName=='all')
        {
            $site = SpaceWorkSite::get();
            return response()->json(['data'=>$site]);
        }
        $site = SpaceWorkSite::where("space_name",$spaceName)->get();
        return response()->json(['data'=>$site]);
    }
	//获取活动场地
    public function GetBigSpace(Request $request)
    {
        $data = $request->input();
        $spaceName = $data['spaceName'];
        if($spaceName=='all')
        {
            $room = SpaceRoom::where("big",'1')->get();
            return response()->json(['data'=>$room]);
        }
        $room = SpaceRoom::where("space_name",$spaceName)->where("big",'1')->get();
        return response()->json(['data'=>$room]);
    }
	//获取会议室状态
    public function GetMeetingRoomStatus(Request $request)
    {
        $data = $request->input();
        $roomName = $data['roomName'];
        $orderDate = $data['orderDate'];
        $checkDate = new Carbon($orderDate);
        $checkDate->hour = 23;
        $checkDate->minute = 59;
        $checkDate->second = 59;
        $endDate = $checkDate->toDateTimeString();
        $status = OrderMeetingRoom::where("room_name",$roomName)->whereBetween('begin_time', [$orderDate, $endDate])->get();
        return response()->json(['data'=>$status]);
    }
	//预定会议室
    public function OrderMeetingRoom(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $roomName = $data['roomName'];
        $name = $data['name'];
        $id_card = $data['id_card'];
        $phone = $data['phone'];
        $date = $data['date'];
        $begin_time = $data['begin_time'];
        $end_time = $data['end_time'];

        $order = new OrderMeetingRoom();
        $order->user_id = $user_id;
        $order->room_name = $roomName;
        $order->name = $name;
        $order->id_card = $id_card;
        $order->phone = $phone;
	$order->date = $date;
        $order->begin_time = $begin_time;
        $order->end_time = $end_time;
        $order->save();

        return response()->json(['data'=>'success']);
    }
	//预定工位
    public function OrderWorkSite(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $siteName = $data['siteName'];
        $name = $data['name'];
        $id_card = $data['id_card'];
        $phone = $data['phone'];
        $number = $data['number'];
	$date = $data['date'];

        $order = new OrderWorkSite();
        $order->user_id = $user_id;
        $order->site_name = $siteName;
        $order->name = $name;
        $order->id_card = $id_card;
        $order->phone = $phone;
        $order->number = $number;
	$order->date = $date;
        $order->save();

        return response()->json(['data'=>'success']);
    }
	//预定活动场地
    public function OrderBigSpace(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $bigSpaceName = $data['bigSpaceName'];
        $name = $data['name'];
        $id_card = $data['id_card'];
        $phone = $data['phone'];
        $hour = $data['hour'];
        $purpose = $data['purpose'];
        $date = $data['date'];

        $order = new OrderBigSpace();
        $order->user_id = $user_id;
        $order->bigspace_name = $bigSpaceName;
        $order->name = $name;
        $order->id_card = $id_card;
        $order->phone = $phone;
        $order->hour = $hour;
        $order->purpose = $purpose;
	$order->date = $date;
        $order->save();

        return response()->json(['data'=>'success']);
    }
	//来访登记
    public function Visitor(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $phone = $data['phone'];
        $space_name = $data['space'];
        $reason = $data['reason'];
        $date = $data['date'];


        $visitor = new Visitor();
        $visitor->user_id = $user_id;
        $visitor->name = $name;
        $visitor->phone = $phone;
        $visitor->space_name = $space_name;
        $visitor->reason = $reason;
        $visitor->date = $date;
        $visitor->save();

        return response()->json(['data'=>'success']);
    }
	//预约来访
    public function Appointment(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
        $phone = $data['phone'];
        $company= $data['company'];
        $space_name = $data['space'];
        $date = $data['date'];
        $time = $data['time'];
        $number = $data['number'];
        $other = $data['other'];


        $visitor = new Appointment();
        $visitor->user_id = $user_id;
        $visitor->name = $name;
        $visitor->phone = $phone;
        $visitor->company = $company;
        $visitor->space_name = $space_name;
        $visitor->date = $date;
        $visitor->time = $time;
        $visitor->number = $number;
        $visitor->other = $other;
        $visitor->save();

        return response()->json(['data'=>'success']);
    }
	//投诉建议
    public function Feedback(Request $request)
    {
        $data = $request->input();
        $space_name = $data['space'];
        $phone = $data['phone'];
        $content = $data['content'];

        $feedback = new Feedback();
        $feedback->space_name = $space_name;
        $feedback->phone = $phone;
        $feedback->content = $content;
        $feedback->save();

        return response()->json(['data'=>$feedback]);
    }
	//获取我的预定
    public function GetMyOrder(Request $request)
    {
        $data =  $request->input();
        $user_id = $data['user_id'];
        $room = OrderMeetingRoom::orderBy('created_at','DESC')->where("user_id",$user_id)->get();
        $big = OrderBigSpace::orderBy('created_at','DESC')->where("user_id",$user_id)->get();
        $site = OrderWorkSite::orderBy('created_at','DESC')->where("user_id",$user_id)->get();
        $visitor = Visitor::orderBy('created_at','DESC')->where("user_id",$user_id)->get();

        return response()->json(['meetingroom'=>$room,'bigspace'=>$big,'worksite'=>$site,'visitor'=>$visitor]);
    }
	//获取服务商类型
    public function GetProviderType()
    {
        $providerType = ProviderType::get();
        return response()->json(['data'=>$providerType]);
    }
	//获取服务商列表
    public function GetProviderList(Request $request)
    {
        $data = $request->input();
        $type_id = $data['id'];
        $providerList = ProviderList::where("type_id",$type_id)->get();
        return response()->json(['data'=>$providerList]);
    }
	//获取服务商详情
    public function GetProviderDetail(Request $request)
    {
        $data = $request->input();
        $provider_id = $data['id'];
        $providerDetail = ProviderDetail::where("provider_id",$provider_id)->get();
        return response()->json(['data'=>$providerDetail]);
    }
	//创新券账户申请
    public function RegistFreeUser(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $name = $data['name'];
	$phone = $data['phone'];
        $company_name = $data['company'];
        $address = $data['address'];
        $number = $data['number'];
        $industry = $data['industry'];
        $img = $data['upImg'];

        $check = FreeUser::where("number",$number)->first();
        if($check)
        {
	    if($check->status=='2')
            {
                $check->user_id = $user_id;
                $check->name = $name;
		$check->phone = $phone;
                $check->company_name = $company_name;
                $check->address = $address;
                $check->number = $number;
                $check->industry = $industry;
                $check->img = $img;
                $check->status = 0;
                $check->save();
                return 1;
            }
            return 0;
	}
        $new = new FreeUser();
        $new->user_id = $user_id;
        $new->name = $name;
	$new->phone = $phone;
        $new->company_name = $company_name;
        $new->address = $address;
        $new->number = $number;
        $new->industry = $industry;
        $new->img = $img;
        $new->save();

        return 1;
    }
	//查询创新券申请状态
    public function GetFreeUser(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];

        $check = FreeUser::where("user_id",$user_id)->first();
	if($check)
        {
            return $check->status;
        }
        return 3;
    }
	//获取创新券详情
public function GetFreeUserDetail(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $user = FreeUser::where("user_id",$user_id)->where("status",1)->first();
        return response()->json(['data'=>$user]);
    }
	//获取创新券订单
    public function CheckFreeBill(Request $request)
    {
        $data = $request->input();
	$user_id = $data['user_id'];
        $company = FreeUser::where(["user_id"=>$user_id,"status"=>1])->first();
        if($company)
	 {
            $id = $company->id;
            $bill = FreeBill::where("company_id",$id)->get();
            return response()->json(['data'=>$bill]);
        }
        return 0;
    }
	//获取个人资料
    public function GetMyProfile(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        if($user_id=='0')
        {
            return 0;
        }
        else
        {
            $user = User::where("id",$user_id)->first();
            return response()->json(['data'=>$user]);
        }
    }
	//设置个人资料
    public function SetMyProfile(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        if($user_id=='0')
        {
            return 0;
        }
        {
            $user = User::where("id",$user_id)->first();
            $company = $data['company'];
            $real_name = $data['real_name'];
            $phone = $data['phone'];

            $user->phone = $phone;
            $user->real_name = $real_name;
            $user->company = $company;
            $user->save();

            return 1;
        }
    }
	//政策咨询
    public function PostPolicy(Request $request)
    {
        $data = $request->input();
        $user_id = $data['user_id'];
        $company_name = $data['company'];
        $name = $data['name'];
        $phone = $data['phone'];

        $policy = new PolicyConsult();
        $policy->user_id = $user_id;
        $policy->company_name = $company_name;
        $policy->name = $name;
        $policy->phone = $phone;
        $policy->save();
        $id = $policy->id;
        
        if($id)
        {
            return 1;
        }else
            return 0;
    }
}
